package com.weng.cloud.service8881.concurrenct.algorithm.recursion.sample;

import com.google.common.collect.Sets;
import com.weng.cloud.service8881.concurrenct.algorithm.recursion.base.Puzzle;

import java.util.Set;

/**
 * @Author: weng
 * @Date: 2022/7/10
 * @Description: com.weng.cloud.service8881.concurrenct.algorithm.recursion.sample
 */

public class DfsMazePuzzle implements Puzzle<Integer, Character> {

    private final Character[] dataSource;
    private final int bgnIdx, endIdx, cols;
    private final int[] oper;
    private final boolean[] isDone;
    private int ans;

    public DfsMazePuzzle(Character[] dataSource, int bgnIdx, int endIdx, int cols) {
        this.dataSource = dataSource;
        this.bgnIdx = bgnIdx;
        this.endIdx = endIdx;
        this.cols = cols;
        this.oper = new int[]{-1, cols, 1};
        this.isDone = new boolean[this.dataSource.length];
    }

    @Override
    public Integer initialPosition() {
        return bgnIdx;
    }

    @Override
    public boolean isGoal(Integer position) {
        return position == endIdx;
    }

    @Override
    public Set<Character> legalMoves(Integer position) {
        Set<Character> moves = Sets.newHashSet();
        int cur = 0, next = 0;
        for (int o : oper) {
            next = cur + o;

            //过滤
            if (next >= this.dataSource.length) continue;    //数组边界判断
            else if (cur % cols == 0 && o == -1) continue;    //数据边界判断
            else if ((cur + 1) % cols == 0 && o == 1) continue;
            else if (isDone[next]) continue;    //后面两条涉及数组边界问题,放在边界判断后面
            else if (this.dataSource[cur] == this.dataSource[next]) continue;

            moves.add(this.dataSource[next]);


//            tmp.add(next);
            isDone[next] = true;
        }
        return null;
    }

    @Override
    public Integer move(Integer position, Character move) {
        return null;
    }
}
